package com.yan.springbootstudy.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;

import com.yan.springbootstudy.common.datasource.MutiDataSource;
/**
 * 
 * @Title: DataSourceConfig.java
 * @Package com.yan.springbootstudy.config
 * @Description: 数据源的配置
 * @author yan
 * @date 2017年8月30日
 * @version V1.0
 */
@Configuration
@Order(1)
public class DataSourceConfig {

	@Bean
	@ConfigurationProperties("datasource1")
	public DataSourceProperties datasource1Properties() {
	    return new DataSourceProperties();
	}

	@Bean
	@ConfigurationProperties("datasource1")
	public DataSource datasource1() {
	    return datasource1Properties().initializeDataSourceBuilder().build();
	}

	@Bean
	@ConfigurationProperties("datasource2")
	public DataSourceProperties datasource2Properties() {
	    return new DataSourceProperties();
	}

	@Bean
	@ConfigurationProperties("datasource2")
	public DataSource datasource2() {
	    return datasource2Properties().initializeDataSourceBuilder().build();
	}
	
	@Bean
	@Primary
	public MutiDataSource  datasource(){
		MutiDataSource mutiDataSource = new MutiDataSource();
		Map<Object,Object>  targetDataSources = new HashMap<Object,Object>();
		targetDataSources.put("datasource1", datasource1());
		targetDataSources.put("datasource2", datasource2());
		mutiDataSource.setTargetDataSources(targetDataSources);
		mutiDataSource.setDefaultTargetDataSource(datasource1());
		return mutiDataSource;
	}
	

}
